'use client'

import { useState, useCallback, useEffect } from 'react'

// 加载状态管理 Hook
export const useLoading = (initialState = false) => {
    const [loading, setLoading] = useState(initialState)
    const [error, setError] = useState(null)

    const startLoading = useCallback(() => {
        setLoading(true)
        setError(null)
    }, [])

    const stopLoading = useCallback(() => {
        setLoading(false)
    }, [])

    const setLoadingError = useCallback((error) => {
        setError(error)
        setLoading(false)
    }, [])

    const withLoading = useCallback(async (asyncFunction) => {
        try {
            startLoading()
            const result = await asyncFunction()
            stopLoading()
            return result
        } catch (error) {
            setLoadingError(error)
            throw error
        }
    }, [startLoading, stopLoading, setLoadingError])

    return {
        loading,
        error,
        startLoading,
        stopLoading,
        setLoadingError,
        withLoading
    }
}

// 多步骤加载状态管理 Hook
export const useStepLoading = () => {
    const [currentStep, setCurrentStep] = useState(0)
    const [totalSteps, setTotalSteps] = useState(0)
    const [stepMessage, setStepMessage] = useState('')
    const [loading, setLoading] = useState(false)
    const [error, setError] = useState(null)

    const startStepLoading = useCallback((steps, initialMessage = '') => {
        setTotalSteps(steps)
        setCurrentStep(0)
        setStepMessage(initialMessage)
        setLoading(true)
        setError(null)
    }, [])

    const nextStep = useCallback((message = '') => {
        setCurrentStep(prev => prev + 1)
        setStepMessage(message)
    }, [])

    const finishLoading = useCallback(() => {
        setLoading(false)
        setCurrentStep(0)
        setStepMessage('')
    }, [])

    const setStepError = useCallback((error) => {
        setError(error)
        setLoading(false)
    }, [])

    const progress = totalSteps > 0 ? Math.round((currentStep / totalSteps) * 100) : 0

    return {
        loading,
        error,
        currentStep,
        totalSteps,
        stepMessage,
        progress,
        startStepLoading,
        nextStep,
        finishLoading,
        setStepError
    }
}

// 异步数据获取 Hook
export const useAsyncData = (asyncFunction, dependencies = []) => {
    const [data, setData] = useState(null)
    const { loading, error, withLoading } = useLoading(true)

    const fetchData = useCallback(async () => {
        try {
            const result = await withLoading(asyncFunction)
            setData(result)
            return result
        } catch (error) {
            console.error('Data fetch error:', error)
            throw error
        }
    }, [asyncFunction, withLoading])

    const refetch = useCallback(() => {
        return fetchData()
    }, [fetchData])

    // 初始加载
    useEffect(() => {
        fetchData()
    }, [fetchData])

    return {
        data,
        loading,
        error,
        refetch
    }
}